using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace Colorprj.Masters
{
    class PartyManager
    {

        public DataSet GetAllParties()
        {
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            string sqlCommand = "SelAllParty";
            DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
            // DataSet that will hold the returned results		
            DataSet productsDataSet = null;

            productsDataSet = db.ExecuteDataSet(selSPCommand);

            // Note: connection was closed by ExecuteDataSet method call 

            return productsDataSet;
        }

        public bool AddParty(Party  pEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "AddParty";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "PartyName", DbType.String, pEntity.PartyName );
                    db.AddInParameter(addGroupCommand, "PartyAddress1", DbType.String, pEntity.PartyAddress1 );
                    db.AddInParameter(addGroupCommand, "PartyAddress2", DbType.String, pEntity.PartyAddress2 );
                    db.AddInParameter(addGroupCommand, "PartyCity", DbType.String, pEntity.PartyCity );
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, pEntity.PhoneNo );
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, pEntity.EmailId );


                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }

        public bool UpdParty(Party pEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "UpdParty";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "PartyId", DbType.String, pEntity.PartyId);
                    db.AddInParameter(addGroupCommand, "PartyName", DbType.String, pEntity.PartyName);
                    db.AddInParameter(addGroupCommand, "PartyAddress1", DbType.String, pEntity.PartyAddress1);
                    db.AddInParameter(addGroupCommand, "PartyAddress2", DbType.String, pEntity.PartyAddress2);
                    db.AddInParameter(addGroupCommand, "PartyCity", DbType.String, pEntity.PartyCity);
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, pEntity.PhoneNo);
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, pEntity.EmailId);


                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }
        
        public void DelParty(int Id)
        {

            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                string sqlCommand = "DelParty";
                DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(selSPCommand, "Id", DbType.Int32, Id);
                db.ExecuteNonQuery (selSPCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }


    }
}
